package com.neusoft.elmboot.controller;

import com.neusoft.elmboot.po.dto.BusinessWalletDTO;
import com.neusoft.elmboot.service.BusinessWalletService;
import com.neusoft.elmboot.util.response.ResultBody;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 商家钱包控制器
 */
@RestController
@RequestMapping("/elm/BusinessWalletController")
public class BusinessWalletController {

    @Resource
    private BusinessWalletService businessWalletService;

    /**
     * 获取商家钱包信息
     */
    @PostMapping("/getWalletInfo")
    public ResultBody getWalletInfo(@RequestParam String businessId) throws Exception {
        BusinessWalletDTO walletInfo = businessWalletService.getBusinessWalletInfo(businessId);
        return ResultBody.success("获取成功", walletInfo);
    }

    /**
     * 获取所有商家钱包信息（管理员使用）
     */
    @PostMapping("/getAllWalletInfo")
    public ResultBody getAllWalletInfo() throws Exception {
        List<BusinessWalletDTO> walletInfoList = businessWalletService.getAllBusinessWalletInfo();
        return ResultBody.success("获取成功", walletInfoList);
    }

    /**
     * 商家钱包提现
     */
    @PostMapping("/withdraw")
    public ResultBody withdraw(@RequestParam String businessId,
                              @RequestParam String payPwd,
                              @RequestParam BigDecimal amount) throws Exception {
        businessWalletService.businessWithdraw(businessId, payPwd, amount);
        return ResultBody.success("提现成功");
    }

    /**
     * 获取商家今日收益统计
     */
    @PostMapping("/getTodayStats")
    public ResultBody getTodayStats(@RequestParam String businessId) throws Exception {
        Map<String, Object> stats = businessWalletService.getTodayRevenueStats(businessId);
        return ResultBody.success("获取成功", stats);
    }

    /**
     * 获取商家月度收益统计
     */
    @PostMapping("/getMonthlyStats")
    public ResultBody getMonthlyStats(@RequestParam String businessId) throws Exception {
        Map<String, Object> stats = businessWalletService.getMonthlyRevenueStats(businessId);
        return ResultBody.success("获取成功", stats);
    }

    /**
     * 获取平台总收益统计（管理员使用）
     */
    @PostMapping("/getPlatformStats")
    public ResultBody getPlatformStats() throws Exception {
        Map<String, Object> stats = businessWalletService.getPlatformTotalStats();
        return ResultBody.success("获取成功", stats);
    }

    /**
     * 获取商家钱包交易记录
     */
    @PostMapping("/getTransactions")
    public ResultBody getTransactions(@RequestParam String businessId,
                                    @RequestParam(defaultValue = "1") Integer page,
                                    @RequestParam(defaultValue = "20") Integer size) throws Exception {
        List<Map<String, Object>> transactions = businessWalletService.getBusinessTransactions(businessId, page, size);
        return ResultBody.success("获取成功", transactions);
    }

    /**
     * 初始化商家钱包
     */
    @PostMapping("/initWallet")
    public ResultBody initWallet(@RequestParam String businessId) throws Exception {
        businessWalletService.initBusinessWallet(businessId);
        return ResultBody.success("钱包初始化成功");
    }

    /**
     * 计算平台手续费
     */
    @PostMapping("/calculateFee")
    public ResultBody calculateFee(@RequestParam BigDecimal orderAmount) throws Exception {
        BigDecimal platformFee = businessWalletService.calculatePlatformFee(orderAmount);
        return ResultBody.success("计算成功", platformFee);
    }
}
